package com.feiyun.erp.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feiyun.erp.entity.Role;
import com.feiyun.erp.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xray
 * @since 2021-05-03
 */
@RestController
public class RoleController {

    public static final long SIZE = 5;

    @Autowired
    private IRoleService roleService;

    /**
     * 分页查询，RestFul GET--> 查询  POST --> 添加 PUT --> 更新 DELETE--> 删除
     * @param current
     * @return
     */
    @GetMapping("/roles/page/{current}")
    public ResponseEntity<IPage<Role>> getRolePage(@PathVariable Integer current){
        Page<Role> page = new Page<>(current,SIZE);
        IPage<Role> rolePage = roleService.getRolePage(page);
        return ResponseEntity.ok(rolePage);
    }

    /**
     * 添加角色
     * @param role Role对象以JSON格式传入，需要加@RequestBody注解
     * @return
     */
    @PostMapping("/role")
    public ResponseEntity<String> saveRole(@RequestBody Role role){
        //生成随机的字符串做id
        role.setId(UUID.randomUUID().toString().replace("-","").substring(0,10));
        roleService.save(role);
        return ResponseEntity.ok("添加成功");
    }

    /**
     * 更新角色 以id为条件，更新其余的字段
     * @param role
     * @return
     */
    @PutMapping("/role")
    public ResponseEntity<String> updateRole(@RequestBody Role role){
        roleService.updateById(role);
        return ResponseEntity.ok("更新成功");
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @DeleteMapping("/role/{id}")
    public ResponseEntity<String> deleteRole(@PathVariable("id") String id){
        roleService.removeById(id);
        return ResponseEntity.ok("删除成功");
    }
}
